//
//  BKSingleCollectionView.m
//  bestkeep
//
//  Created by utouu_mhm on 16/11/15.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKSingleCollectionView.h"
#import "BKSingleCollectionViewCell.h"
#import "BKClassifyNoDataFooter.h"

#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1]
static NSString *identifier = @"KSingleCollectionViewCell";

@implementation BKSingleCollectionView

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        self.backgroundColor = RGB(242, 242, 242);
        self.dataSource =self;
        self.delegate =self;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        [self registerClass:[BKSingleCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        [self registerClass:[BKClassifyNoDataFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[NSString stringWithFormat:@"%@footer",NSStringFromClass([BKSingleCollectionView class])]];
        
    }
    return self;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.singleArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BKSingleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.singleGoodsModel = self.singleArray[indexPath.row];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ScreenWidth, 117);

}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
-(BKClassifyNoDataFooter *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    BKClassifyNoDataFooter *reusebleView = nil;
    
    if (kind == UICollectionElementKindSectionFooter && self.isNoData) {
        
        reusebleView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:[NSString stringWithFormat:@"%@footer",NSStringFromClass([BKSingleCollectionView class])] forIndexPath:indexPath];
    }
    return reusebleView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (self.isNoData) {
        
        return CGSizeMake(ScreenWidth, 85);
    }
    
    return CGSizeZero;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.singleDelegate respondsToSelector:@selector(BKCollectionViewWithIndexPath:)]) {
        
        [self.singleDelegate BKCollectionViewWithIndexPath:indexPath];
    }
}
#pragma mark - 滚动的范围
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([self.singleDelegate respondsToSelector:@selector(tableviewCntentOffset:)]) {
        
        [self.singleDelegate tableviewCntentOffset:scrollView.contentOffset];
    }
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y == 0) {
        
        return;
    }
    
    
    if ([self.singleDelegate respondsToSelector:@selector(BKCollectionViewWithContentOffset:)]) {
        
        [self.singleDelegate BKCollectionViewWithContentOffset:velocity];
    }
}

#pragma mark - setSingleArray方法
-(void)setSingleArray:(NSArray *)singleArray
{
    _singleArray = singleArray;
    [self reloadData];
}
-(void)setIsNoData:(BOOL)isNoData
{
    _isNoData = isNoData;
    [self reloadData];
}
@end
